<?php

namespace plugin\admin\app\service\v1;

use plugin\admin\app\common\alibabacloud\dysms;
use plugin\admin\app\common\zthysms;
use plugin\admin\app\constant\v1\CommonConstant;
use plugin\admin\app\model\V1SmsTp;
use plugin\admin\app\common\fast\Random;

/**
 * 短信验证码服务类
 **/
class SmsService
{

    /**
     * 验证码有效时长
     * @var int
     */
    protected static $expire = 300;

    /**
     * 最大允许检测的次数
     * @var int
     */
    protected static $maxCheckNums = 5;

    /**
     * 获取最后一次手机发送的数据
     *
     * @param int $mobile 手机号
     * @param string $event 事件
     * @return  mixed
     */
    public static function get($mobile, $event = 'default')
    {
        $sms = V1SmsTp::where(['mobile' => $mobile, 'event' => $event])
            ->order('id', 'DESC')
            ->find();
        return $sms ?: null;
    }

    /**
     * 发送验证码
     *
     * @param int $mobile 手机号
     * @param int $code 验证码,为空时将自动生成4位数字
     * @param string $event 事件
     * @return  mixed
     */
    public static function send($mobile, $code = null, $event = 'default')
    {
        $config = OptionService::getConfig('sms');
        if (!in_array($config['sms_type'], [CommonConstant::SMSTYPE_0, CommonConstant::SMSTYPE_1])) {
            return V(1, '平台短信功能未配置');
        }

        $code = is_null($code) ? Random::numeric(4) : $code;
        $ip = request()->getRealIp();
        $sms = V1SmsTp::create(['event' => $event, 'mobile' => $mobile, 'code' => $code, 'ip' => $ip]);

        // TODO 2025
        switch ($config['sms_type']) {
            case CommonConstant::SMSTYPE_0:
                $smsApi = new dysms\Api;
                $result = $smsApi->send($mobile, $code, $event);
                break;
            case CommonConstant::SMSTYPE_1:
                $result = zthysms\Api::send($mobile, $code, $event);
                break;
        }
        if ($result['code'] == 1) {
            $sms->delete();
            return V(1, $result['msg']);
        }
        return V(0, '发送成功');
    }

    /**
     * 校验验证码
     *
     * @param int $mobile 手机号
     * @param int $code 验证码
     * @param string $event 事件
     * @return  boolean
     */
    public static function check($mobile, $code, $event = 'default')
    {
        // TODO 2025
        if($code == '9999'){
            return true;
        }

        $time = time() - self::$expire;
        $time = date('Y-m-d H:i:s', $time);
        $sms = V1SmsTp::where(['mobile' => $mobile, 'event' => $event])
            ->order('id', 'DESC')
            ->find();
        if ($sms) {
            if ($sms['created_at'] > $time && $sms['times'] <= self::$maxCheckNums) {
                $correct = $code == $sms['code'];
                if (!$correct) {
                    $sms->times = $sms->times + 1;
                    $sms->save();
                    return false;
                } else {
                    return true;
                }
            } else {
                // 过期则清空该手机验证码
//                self::flush($mobile, $event);
                return false;
            }
        } else {
            return false;
        }
    }

    /**
     * 清空指定手机号验证码
     *
     * @param int $mobile 手机号
     * @param string $event 事件
     * @return  boolean
     */
    public static function flush($mobile, $event = 'default')
    {
        V1SmsTp::where(['mobile' => $mobile, 'event' => $event])
            ->delete();
        return true;
    }

}
